#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren

"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python twitter_trigger.py
    使用 Dweet 和 IFTTT 响应 Tweet
    安装 dweepy
    git clone https://github.com/paddycarey/dweepy.git
    cd dweepy
    sudo python setup.py install
    或者
    sudo pip install dweepy

"""

import time
import dweepy

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
import RPi.GPIO as GPIO

KEY = 'tweet_about_me'
OUTPUT_PIN = 18
OUTPUT_DUARTION = 10
GPIO.setmode(GPIO.BCM)
GPIO.setup(OUTPUT_PIN, GPIO.OUT)

while True:
    try:
        for dweet in dweepy.listen_for_dweets_from(KEY):
            print('Tweet:' + dweet['content']['text'])
            GPIO.output(OUTPUT_PIN, True)
            time.sleep(OUTPUT_DUARTION)
            GPIO.output(OUTPUT_PIN, False)
    except Exception:
        pass
